<?php
/**
 * Created by PhpStorm.
 * User: geyouwen
 * Date: 16/7/15
 * Time: 上午10:15
 */

namespace Superadmin\Controller;


use Think\Exception;

class SettingsController extends BaseController
{

    private $mod;
    public function __construct()
    {
        parent::__construct();
        $this->mod = D('settings');
    }

    //核心设置
    public function index()
    {
        $list =  $this->mod->getAllSiteSettings();
        if(IS_AJAX)
        {
            $post = $_POST;
            foreach($post as $key => $val) {
                $data[$key] = serialize(I($key)); //对数据进行过滤
            }
            foreach($data as $key => $val) {
                //待更新数据
                if(isset($list[$key]))
                {
                    $save[$key] = $val;
                } else{
                    //待插入数据
                    $add[] = array(
                      'name' => $key,
                      'data'=> $val,
                    );
                }
            }
            try{
                if(isset($add))
                {
                    //insert action
                    $this->mod->addAll($add);
                }
                if(isset($save))
                {
                    //update action
                    foreach($save as $k => $v)
                    {
                        $this->mod->where(array('name'=>$k))->save(array('data'=>$v));
                    }
                }

            } catch (Exception $e)
            {
                $return = array('status' => 0, 'msg' => L('operation_error'));
                $this->ajaxReturn($return);
            }
            $return = array('status' =>1, 'msg' => L('operation_success'));
            $this->ajaxReturn($return);
        }

        $this->assign('list', $list);
        $this->display();
    }


    //IP黑名单设置
    public function blackip()
    {
        if(IS_AJAX)
        {
            //数据过滤
            $ip = I('ip');
            if($ip)
            {
                $ip = explode(',', $ip);
                foreach($ip as $key => $val)
                {
                   //对ip进行验证
                   $reg = "/^(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])\.(\d{1,2}|1\d\d|2[0-4]\d|25[0-5])$/";
                   if(!preg_match($reg, trim($val))){
                       unset($ip[$key]);
                   } else {
                       $ip[$key] = trim($val);
                   }
                }
                if($ip)
                {
                    $ip = array_unique($ip);
                    foreach($ip as $key => $val)
                    {
                        $data[] = array('ip' => $val);
                    }
                    //删除所有黑名单
                    M('black_ip')->where(1)->delete();
                    //插入新的IP数据
                    D('black_ip')->addAll($data);
                    //将IP存入缓存
                    S('black_list', $ip);
                } else {
                    //删除所有黑名单 过滤之后没有正确的值
                    D('black_ip')->where(1)->delete();
                    //将IP缓存清空
                    S('black_list', null);
                }
            } else {
                //删除所有黑名单 清空所有黑名单
                D('black_ip')->where(1)->delete();
                //将IP缓存清空
                S('black_list', null);
            }
            $return = array('status' =>1, 'msg' => L('operation_success'));
            $this->ajaxReturn($return);
        }
        $black_list = M('black_ip')->select();
        if($black_list) $black_list = implode(",", i_array_column($black_list, 'ip'));
        $this->assign('black_list', $black_list);
        $this->display();
    }

    public function unabledwords()
    {
        //敏感词提交
        if(IS_AJAX)
        {
            $words = trim(I('words'));
            if(!$words)
            {
                $return = array(
                    'status' => 0,
                    'msg'    => '请输入敏感词',
                );
                $this->ajaxReturn($return);
            }
            //检测敏感词是否存在
            $where = array('words' => $words);
            $find  = M('unabledwords')->where($where)->getField('id');
            if($find)
            {
                $return = array(
                    'status' => 0,
                    'msg'    => '敏感词已经存在了',
                );
                $this->ajaxReturn($return);
            }
            M('unabledwords')->add($where);
            $return = array(
                'status' => 1,
                'msg'    => L('operation_success'),
            );
            $this->ajaxReturn($return);
        }
        $list = M('unabledwords')->select();
        $class_list = array(
            'label-default','label-primary','label-success','label-info','label-warning','label-danger'
        );
        $this->assign('list', $list);
        $this->assign('class_list', $class_list);
        $this->display();
    }

    //发送测试邮件
    public function email_test()
    {
        $to_email = I('email');
        if(D('settings')->sendMail($to_email,'测试邮件','测试内容'))
        {
            $return   = array(
                'status' => 1,
                'msg'    => L('发送成功'),
            );
        } else
        {
            $return   = array(
                'status' => 0,
                'msg'    => L('send_wrong'),
            );
        }
        $this->ajaxReturn($return);
    }

    //删除敏感词
    public function del_words()
    {
        if(IS_AJAX)
        {
            $id = I('id');
            D('unabledwords')->where(array('id' => $id))->delete();
            $back = array(
                'status' => 1,
                'msg'    => L('operation_success'),
            );
            $this->ajaxReturn($back);
        }
    }

}